import {Component} from "@angular/core";
import {IonicPage, NavController, NavParams} from "ionic-angular";
import {HttpClient} from "@angular/common/http";
import {AppUrl} from "../../../../app/app.general";

@IonicPage()
@Component({
  templateUrl:'staff-addorchange.html',
  selector:'staff-addorchange-page'
})

export class StaffAddorchangePage{

  staff= {
    userid:'',
    username: '',
    password: '',
    pwdchgflag: false,
    phonenum: null,
    name: '',
    certificatetype: '',
    certification: '',
    roletype: 'STAFF',
    del:null,
    abil:[]
};

  ability:any=[];
  flag:any;
  callback:any;

  constructor(private navP:NavParams,private http:HttpClient,private navCtrl:NavController){
    this.flag=this.navP.get('flag');
    this.callback=this.navP.get('callback');

    if(this.flag==1){
      //modify
      this.staff.userid=this.navP.get('userid');
      this.http.post(AppUrl.user.getstaff,{userid:this.staff.userid}).subscribe(data=>{
        console.log(data);
        let temp:any=data;
        if(temp.code==200){
          let tempstaff:any=temp.data[0];
          let tempstaffability:any=temp.data[1];
          let tempability:any=temp.data[2];

          this.staff.username=tempstaff.username;
          this.staff.certificatetype=tempstaff.certificatetype;
          this.staff.certification=tempstaff.certification;
          this.staff.name=tempstaff.name;
          this.staff.phonenum=tempstaff.phonenum;
          this.staff.roletype=tempstaff.roletype;
          this.staff.del='0';
          for(let ab of tempability){
            let index=tempstaffability.findIndex(e=>e.value==ab.value);
            if(index!=-1){
              this.ability.push({
                value:ab.value,
                label:ab.label,
                select:true
              });
            }else{
              this.ability.push({
                value:ab.value,
                label:ab.label,
                select:false
              });
            }
          }

          for(let tsb of tempstaffability){
            this.staff.abil.push(tsb.value);
          }
        }
      });
    }else{
      this.http.post(AppUrl.user.selectAllability,{}).subscribe(data=>{
        let temp:any=data;
        for(let ab of temp.data){
          this.ability.push({
            value:ab.value,
            label:ab.label,
            select:false
          });
        }
      });
    }
  }

  commit(){
    console.log(this.staff);
    if(this.flag==1){
      //modify old staff
      // this.http.post(AppUrl.user.updStaff,this.staff).subscribe(data1=>{
      this.http.post(AppUrl.user.updStaff,this.staff).subscribe(data=>{
        console.log(data);
        let temp:any=data;
        if(temp.code==200){
          this.callback({username:this.staff.username,userid:this.staff.userid,phonenum:this.staff.phonenum,name:this.staff.name}).then(() => {
            this.navCtrl.pop();
          });
        }
      });
    }else{
      //add new staff
      this.http.post(AppUrl.user.signStaff,this.staff).subscribe(data=>{
        console.log(data);
        let temp:any=data;
        if(temp.code==200){
          this.callback({username:this.staff.username,userid:temp.data.userid,phonenum:this.staff.phonenum,name:this.staff.name}).then(()=>{
            this.navCtrl.pop();
          });
        }
      });
    }
  }
}
